package Server;

import java.util.PriorityQueue;

import org.apache.mina.core.session.IoSession;

public class Subscription extends Thread{
	IoSession session;
	PriorityQueue<String> queue;
	boolean canSend=true;
	final int LIMIT=30;

	public Subscription(IoSession s){
		if(s!=null){
			init(s);
		}
	}
	public void init(IoSession s){
		session=s;
		queue=new PriorityQueue<String>();
		start();
	}
	public void stopMe(){
		stop();
	}
	public IoSession getSession(){
		return session;
	}
	public void add(String s){
		try{
			if(queue.size()>=LIMIT){
				session.close();
			}else{
				System.out.println("q: "+queue);
				queue.add(s);
			}
		}
		catch(Exception e){
			session.close();
		}
	}
	public void run(){
		while(true){
			synchronized (this) {
				if((!queue.isEmpty())&&canSend){
					session.write(queue.poll());
					//canSend=false;	
				}
			}
				

		}
	}
}
